package com.appspot.thinkhea.view;

import java.util.Vector;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.appspot.thinkhea.R;
import com.appspot.thinkhea.model.Picture;
import com.appspot.thinkhea.model.Player;
import com.appspot.thinkhea.model.Record;

public class GridRecordAdapter extends BaseAdapter {
	private LayoutInflater mInflater;
	private Vector<Picture> v;
	private Context context;
	private Record r;
	private Record ir;
	private String tag ="GridRecordAdapter";
	public GridRecordAdapter(Context context, Vector<Picture> v, Record r) {
		this.v = v;
		this.context = context;
		this.r = r;
		setInitRecord(r);
		mInflater = LayoutInflater.from(context);
	}
	
	public void setInitRecord(Record r){
		Vector<Player> v =r.getPlayerList();
		if(ir==null){
			ir = new Record();			
		}else{
			ir.getPlayerList().clear();
		}
		Vector<Player> n = ir.getPlayerList();
		for(int c=0;c<v.size();c++){
			Player k = new Player();
			k.setId(v.get(c).getId());
			k.setValue(v.get(c).getValue());
			n.add(k);
		}
	}
	
	@Override
	public int getCount() {
		return v.size();
	}

	@Override
	public Picture getItem(int position) {
		return v.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.grid_item_record, null);
			holder = new ViewHolder();
			holder.name = (TextView) convertView.findViewById(R.id.Name);
			holder.value = (TextView) convertView.findViewById(R.id.Value);
			holder.diff = (TextView) convertView.findViewById(R.id.Diff);
			holder.image = (ImageView) convertView.findViewById(R.id.Image);
			convertView.setTag(holder);
		} else {
			// reuse convertView
			holder = (ViewHolder) convertView.getTag();
		}
		
		int pid = v.get(position).getId();
		int pval = 0;
		int pvalDifferent = 0;
		String mName = v.get(position).getName();
		Bitmap mImage = v.get(position).getImage();
		if (mImage != null) {
			mImage = Bitmap.createScaledBitmap(mImage, 48, 65, false);
		} else {
			mImage = BitmapFactory.decodeResource(this.context.getResources(),
					R.drawable.unknown);
		}
		Vector<Player> p = r.getPlayerList();
		Vector<Player> ip = ir.getPlayerList();
		Log.i(tag, "getView "+mName);
		for(int a =0;a<p.size();a++){
			int cid = p.get(a).getId();
			if(pid==cid){
				int val = p.get(a).getValue();
				Log.i(tag, "old:"+ip.get(a).getValue()+" new:"+val);
				pval = val;
				pvalDifferent = val-ip.get(a).getValue();
			}
		}
		Log.i(tag, "getView-end");
		holder.name.setText(mName);
		holder.value.setText(pval+" ");
		if(pval<0){
			holder.value.setTextColor(R.color.color_red);
		}else{
			holder.value.setTextColor(R.color.color_green);
		}
		if(pvalDifferent<0){
			holder.diff.setText("["+pvalDifferent+"] ");
			holder.diff.setTextColor(R.color.color_red);
		}else{
			holder.diff.setText("[+"+pvalDifferent+"] ");
			holder.diff.setTextColor(R.color.color_green);
		}
		holder.image.setImageBitmap(mImage);
		return convertView;
	}

	private class ViewHolder {
		TextView name;
		TextView value;
		TextView diff;
		ImageView image;
	}
}